package com.example.yskjproject.ui.activity;


import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.listener.CustomListener;
import com.bigkoo.pickerview.listener.OnTimeSelectListener;
import com.bigkoo.pickerview.view.TimePickerView;
import com.example.yskjproject.R;
import com.example.yskjproject.base.BaseActivity;
import com.example.yskjproject.content.EventContent;
import com.example.yskjproject.databinding.ActivityBirthdayAlertBinding;
import com.example.yskjproject.event.EventMessage;
import com.example.yskjproject.http.Urls;
import com.example.yskjproject.http.callback.JsonCallback;
import com.example.yskjproject.model.LzyResponse;
import com.example.yskjproject.utils.EventBusUtil;
import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;
import com.tencent.qcloud.tuicore.util.ToastUtil;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import okhttp3.MediaType;
import okhttp3.RequestBody;

public class BirthdayAlertActivity extends BaseActivity<ActivityBirthdayAlertBinding> {
    private ActivityBirthdayAlertBinding mActivityBirthdayAlertBinding;
    private TimePickerView pvTime;
    private Gson gson = new Gson();

    @Override
    protected ActivityBirthdayAlertBinding getViewBinding() {
        mActivityBirthdayAlertBinding = ActivityBirthdayAlertBinding.inflate(LayoutInflater.from(this));
        return mActivityBirthdayAlertBinding;
    }

    @Override
    protected void initData() {
        setTitle("编辑生日");
        mActivityBirthdayAlertBinding.llBirthday.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showTimePicker();
                pvTime.show();
            }
        });
    }

    //显示时间
    public void showTimePicker() {
        Calendar selectedDate = Calendar.getInstance();//系统当前时间
        Calendar startDate = Calendar.getInstance();
        startDate.set(1800, 1, 23);
        Calendar endDate = Calendar.getInstance();
        endDate.set(2027, 2, 28);
        pvTime = new TimePickerBuilder(this, new OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date, View v) {//选中事件回调
                mActivityBirthdayAlertBinding.tvBirthdayInfo.setText(getTime(date));
                updateBirthdayInfo();
                EventMessage eventMessage = new EventMessage(EventContent.update_birthday, mActivityBirthdayAlertBinding.tvBirthdayInfo.getText().toString());
                EventBusUtil.sendEvent(eventMessage);
            }
        })
                .setDate(selectedDate)
                .setRangDate(startDate, endDate)
                .setLayoutRes(R.layout.timer_picker, new CustomListener() {

                    @Override
                    public void customLayout(View v) {
                        final TextView tvSubmit = v.findViewById(R.id.tv_finish);
                        TextView ivCancel = v.findViewById(R.id.tv_cancel);
                        tvSubmit.setOnClickListener(v1 -> {
                            pvTime.returnData();
                            pvTime.dismiss();
                        });
                        ivCancel.setOnClickListener(v12 -> pvTime.dismiss());
                    }
                })
                .setContentTextSize(36)
                .setType(new boolean[]{true, true, true, false, false, false})
                .setLabel("年", "月", "日", "时", "分", "秒")
                .setLineSpacingMultiplier(3f)
                .setTextXOffset(40, 40, 40, 0, 0, 0)
                .isCenterLabel(false) //是否只显示中间选中项的label文字，false则每项item全部都带有label。
                .build();



    }

    private void updateBirthdayInfo() {
        Map<String, Object> jsonParams = new HashMap<>();
        jsonParams.put("borthday", mActivityBirthdayAlertBinding.tvBirthdayInfo.getText().toString());
        String paramsDate = gson.toJson(jsonParams);
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), paramsDate);
        OkGo.<LzyResponse<Void>>post(Urls.UPDATE_USER).upRequestBody(requestBody).execute(new JsonCallback<LzyResponse<Void>>() {
            @Override
            public void onSuccess(Response<LzyResponse<Void>> response) {
                ToastUtil.toastLongMessage("修改生日成功");
                EventMessage eventMessage = new EventMessage(EventContent.update_height, mActivityBirthdayAlertBinding.tvBirthdayInfo.getText().toString());
                EventBusUtil.sendEvent(eventMessage);
                finish();
            }

            @Override
            public void onError(Response<LzyResponse<Void>> response) {
                super.onError(response);
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventMessage eventMessage = new EventMessage(EventContent.update_birthday, mActivityBirthdayAlertBinding.tvBirthdayInfo.getText().toString());
        EventBusUtil.sendEvent(eventMessage);
    }

    private String getTime(Date date) {//可根据需要自行截取数据显示
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        return format.format(date);
    }
}
